#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include "main_win.h"
#include "digital.h"

#define TIPS "请输入数字"
#define MESG "请输入1至5的整数！"
#define WIDTH 600
#define HEIGHT 500

static GtkWidget *window;
static GtkTextBuffer *buffer;
static GtkTextIter iter;
static count = 0;

static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data){
	return FALSE;
}

static void destroy(GtkWidget *widget, gpointer data){
	gtk_widget_show (m_window);
	gtk_widget_hide (window);
}

static GtkWidget* init_text_view(GtkTextBuffer **buffer){
	GtkWidget *text_view = gtk_text_view_new();
	*buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
	
	return text_view;
}

static void clicked(GtkWidget *widget, GtkEntry *entry){
	GtkWidget *mesg;
	char *text;
	int num, n = 0;

	count = 0;

	GtkTextIter start, end;
	gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
	gtk_text_buffer_get_iter_at_offset (buffer, &end, -1);
	gtk_text_buffer_delete (buffer, &start, &end);

	text = (char *)gtk_entry_get_text (entry);

	num = digital (text, &n);

	if(num > 0 && num <= 5)
		deal (num, buffer);
	else{
		mesg = gtk_message_dialog_new ((GtkWindow *)window, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_NONE, MESG);
		gtk_widget_show (mesg);
	}
}
	
/*界面，代码真多*/
int s_win(void){
	GtkWidget *separator, *tips_label, *entry, *button, *vbox, *hbox, *text_view;

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW(window), SORT);
	gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
	gtk_window_set_icon_from_file (GTK_WINDOW(window), LOGO, NULL);
	gtk_window_set_default_size (GTK_WINDOW(window), 1, 1);
	gtk_window_set_resizable (GTK_WINDOW(window), FALSE);

	g_signal_connect (window, "delete-event", G_CALLBACK (delete_event), NULL);
	g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);

	tips_label = gtk_label_new_with_mnemonic (TIPS);

	entry = gtk_entry_new();

	hbox = gtk_hbox_new (FALSE, 0);
	vbox = gtk_vbox_new (FALSE, 0);

	button = gtk_button_new_with_label ("确定");
	g_signal_connect (button, "clicked", G_CALLBACK (clicked), entry);

	separator = gtk_hseparator_new ();

	gtk_box_pack_start (GTK_BOX (hbox), tips_label, FALSE, FALSE, 0);
	gtk_widget_show (tips_label);

	gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0);
	gtk_widget_show (entry);
	
	gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
	gtk_widget_show (button);
	
	gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
	gtk_widget_show (hbox);
	
	gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
	gtk_widget_show (separator);

	text_view = init_text_view (&buffer);
	gtk_widget_set_size_request(text_view, WIDTH, HEIGHT);
	gtk_box_pack_start (GTK_BOX (vbox), text_view, FALSE, FALSE, 0);
	gtk_widget_show (text_view);

	gtk_container_add (GTK_CONTAINER (window), vbox);
	gtk_widget_show (vbox);

	gtk_widget_show (window);

	return 0;
}

/*显示结果。在deal()调用*/
int sort_prin(char *head, char ch, int size, GtkTextBuffer *buffer){
	int len, i = 0;

	char *p_str = (char *)malloc(sizeof(char) * size);

	bzero (p_str, sizeof(char) * size);

	strcat (p_str, head);
	strncat (p_str, &ch, 1);
	
	ch = '\t';
	count++;

	if(count >= WIDTH/11/size){
		ch = '\n';
		count = 0;
	}

	strncat (p_str, &ch, 1);

	gtk_text_buffer_get_iter_at_offset (buffer, &iter, -1);
        gtk_text_buffer_insert (buffer, &iter, p_str, -1);

	free (p_str);

	return 0;
}
